﻿/*
// Exepack.NET
// http://www.codeplex.com/exepack
//
// PE header helper
// Written by Y [02-03-09]
// Copyright (c) 2008-2009 Alexey Yakovlev
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Exepack
{
	/// <summary>
	/// PE Executable File Helper
	/// </summary>
	static class PeHeaderHelper
	{
		/// <summary>
		/// Returns true if the given file is a console application
		/// </summary>
		public static bool IsConsoleApplication(string fileName)
		{
			using (FileStream fs = File.OpenRead(fileName))
			using (BinaryReader reader = new BinaryReader(fs))
			{
				// read PE offset
				fs.Position = 0x3C;
				uint peOffset = reader.ReadUInt32();

				// jump to PE header subsystem flag
				fs.Position = peOffset + 0x5c;
				byte subsystem = reader.ReadByte();

				// 2 = GUI, 3 = console
				return subsystem == 3;
			}
		}
	}
}
